<?php
/**
 * 系统非常规MD5加密方法
 * @param  string $str 要加密的字符串
 * @return string 
 */
function mt_data_md5($str, $key = 'MatiData'){
	return '' === $str ? '' : md5(sha1($str) . $key);
}
/**
 * 系统生成用户token方法
 * @return string 
 */
function mt_data_token(){
	return strtoupper(substr(sha1(uniqid(NULL, TRUE)) . sha1(uniqid(NULL, TRUE)), 0, 64));
}
/**
 * session持续时间
 * @return string 
 */
function mt_data_expires($expire_time){
	return time()+$expire_time;
}
/**
 * 获取客户端信息
 * @return string 
 */
function mt_data_client(){
        $app_key = I('get.app_key');
        if(!empty($app_key)) {
            $flag = substr($app_key, 0, 1);
            $client_sources = array('a' => 'Android','i' => 'iOS');
            return array_key_exists($flag, $client_sources) ?  $client_sources[$flag] : 'unknown';
        }
	return 'unknown';
}
// 获取随机的字符串，用于token，EncodingAESKey等的生成
function get_rand_char($length = 6) {
	$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
	$strLength = 61;
	
	for($i = 0; $i < $length; $i ++) {
		$res .= $str [rand ( 0, $strLength )];
	}
	
	return $res;
}
function sms($mobile,$content)
{
    $url="http://121.199.48.186:1210/services/msgsend.asmx/SendMsg?userCode=MTSJ&userPass=MTSJ123&DesNo={$mobile}&Msg={$content}&Channel=0";
    $result_xml = file_get_contents($url);
    $xml = simplexml_load_string($result_xml);
    return (string)$xml[0]; 
}
function getPeriod(){
    return date('Ym',time());
}
/**
 * 返回加字母的股票代码
 * @param type $stockcode
 */
function getStockWithFlag($stockcode){
    
    if(!preg_match("/^sh|sz/i", $stockcode)){
          if(substr($stockcode, 0,1)=='6') $stockcode='sh'.$stockcode;
          else $stockcode='sz'.$stockcode; 
    }
    return strtolower($stockcode);
}
/**
* 个股实时股票行情
*/
function getRealTimeStockInfo($stockcode){

    $url = "http://qt.gtimg.cn/q={$stockcode}";
            
    $result = \Org\Net\Http::fsockopenDownload($url);
    $result= mb_convert_encoding($result,"utf-8","gb2312");
    $params =  split('~', substr($result,  strpos($result, '=')+2));

    if(count($params)<>50) return false;

    $data=array();

    $data['stock_code'] = $stockcode;
    $data['stock_name'] = $params[1];
    $data['open']=$params[5];
    $data['close']=$params[4];
    $data['current']=$params[3];
    $data['high']=$params[33];
    $data['low']=$params[34];
    $data['volume']=$params[36];
    $data['trunover']=$params[37];
    $data['chg']=  $params[31];
    $data['percent']=$params[32];
    $data['update_time'] =  NOW_TIME;
    return $data;
    }
//function prizeCounter($param){
//        $redis = new \Redis();
//        $redis->connect('127.0.0.1', 6379);
//}
/**
 * 添加任务
 * @param type $uid
 * @param type $score
 * @param type $cash_flow_id
 * @param type $exchange_type_id
 * @param type $content
 */
function add_task($task_type,$uid,$cash_flow_id=0,$desc="") {

    $where =array(
            'task_type'=>$task_type,
            'status'=>1,
            'deadline'=>array(array('eq',0),array('gt',NOW_TIME),'or')
            );
        
    $task = M('Task')->where($where)->cache(true,3600)->find();

    if(empty($task))  return false;
    
    $add_method = 'add_'.$task_type;
    $result = D('UserTask')->$add_method($task['id'],$uid,$task_type);

    if($result < 0) return false;

    add_money_log($uid, $task['money'], $cash_flow_id, 5, $desc);
    
    return true;
}

/**
 * 添加人民币消费流水
 * @param type $uid
 * @param type $score
 * @param type $cash_flow_id
 * @param type $exchange_type_id
 * @param type $content
 */
function add_money_log($uid,$score,$cash_flow_id,$exchange_type,$content='') {
	$cash_flows=['income','pay'];
        $data ['user_id'] = $uid;
	$data ['money'] = $score;
        $data ['content'] = $content;
        $data ['cash_flow'] = $cash_flows[$cash_flow_id];
        $data ['exchange_type'] = $exchange_type;
	D('UserMoneyLog')->log($data);
}
/**
 * 添加经验值消费日志
 * @param type $uid
 * @param type $score
 * @param type $cash_flow_id
 * @param type $exchange_type_id
 * @param type $content
 */
function add_experience_log($uid,$experience,$cash_flow_id,$exchange_type,$content='') {
	$cash_flows=['income','pay'];
        $data ['user_id'] = $uid;
	$data ['experience'] = $experience;
        $data ['content'] = $content;
        $data ['cash_flow'] = $cash_flows[$cash_flow_id];
        $data ['exchange_type'] = $exchange_type;
	D('UserExperienceLog')->log($data);
}

/**
 * 经验值兑换钱
 * @param type $uid
 * @param type $coin
 * @param type $content
 */
function exchange_money($uid,$money,$content='') {
        $data ['user_id'] = $uid;
	$data ['money'] = $money;
        $data ['content'] = $content;
        $data ['cash_flow'] = 'income';
        $data ['exchange_type'] = 'exchange';
	D('UserMoneyLog')->log($data);
}
/**
 * 时间提醒
 * @param type $time
 * @return type
 */
function tranTime($time) { 
    $rtime = date("m-d H:i",$time); 
    $rtime2 = date("Y-m-d H:i",$time);
    $htime = date("H:i",$time);  
    $time = time() - $time; 
    if ($time < 60) {  
        $str = '刚刚';  
    } elseif ($time < 60 * 60) {  
        $min = floor($time/60);
        $str = $min.' 分钟前';  
    }elseif ($time < 60 * 60 * 24) {  
        $h = floor($time/(60*60));  
        $str = $h.'小时前 '.$htime; 
    }elseif ($time < 60 * 60 * 24 * 3) {  
        $d = floor($time/(60*60*24));  
        if($d==1)  
           $str = '昨天 '.$htime;  
        else 
           $str = '前天 '.$htime;  
    }elseif ($time < 60 * 60 * 24 * 7) {
        $d = floor($time/(60*60*24));  
        $str = $d.' 天前 '.$htime;  
 } elseif ($time < 60 * 60 * 24 * 30) {
      $str = $rtime;  
 }else {  
        $str = $rtime2;   
    }  
    return $str;  
}